package com.note.syschronizedMoudule;

import javax.annotation.security.RunAs;

/**
 * @ClassName SychronizedMethod
 * @Description TODO
 * @Author QiBin
 * @Date 2020/6/1716:42
 * @Version 1.0
 * 对象锁示例  代码块形式
 **/
public class SychronizedMethod implements Runnable {
    static SychronizedMethod instance = new SychronizedMethod();


    Object lock1 = new Object();
    Object lock2 = new Object();

    @Override
    public void run() {
        synchronized (this){
            System.out.println("我是对象锁的代码块形式.我叫++"+Thread.currentThread().getName());
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+"===运行结束");
        }
        synchronized (lock1){
            System.out.println("我是lock1的代码块形式.我叫++"+Thread.currentThread().getName());
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+"===运行结束");
        }
        synchronized (lock2){
            System.out.println("我是lock2的代码块形式.我叫++"+Thread.currentThread().getName());
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+"===运行结束");
        }
    }

    public static void main(String[] args) {
        Runnable target;
        Thread t1 = new Thread(instance);
        Thread t2 = new Thread(instance);
        t1.start();
        t2.start();

        while (t1.isAlive() || t2.isAlive()){

        }
        System.out.println("finished");

    }

}
